﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Windows.Input;
using System.Windows.Controls;
using System.ServiceModel.DomainServices.Client;
using GalaSoft.MvvmLight.Command;
using EnsureWeb.UI.Core;
using EnsureWeb.Web.DataModel;
using EnsureWeb.Controls;

namespace EnsureWeb.UI.ViewModels
{
    /// <summary>
    /// LoginViewModel,  "[StaticResource("LoginViewModel")]" will write a staticResouce to app.resouces
    /// </summary>
    [StaticResource("LoginViewModel")]
    public partial class LoginViewModel : EditableViewModel
    {
        /// <summary>
        /// key:power name, value:has power
        /// </summary>
        private Dictionary<string,bool> _currentPowers;
        public Dictionary<string,bool> CurrentPowers
        {
            get
            {
                return _currentPowers;
            }
            set
            {
                _currentPowers = value;
                RaisePropertyChanged("CurrentPowers");
            }
        }

        /// <summary>
        /// CurrentUser info
        /// </summary>
        private MembershipUser _currentUser = new MembershipUser();
        public MembershipUser CurrentUser
        {
            get
            {
                return _currentUser;
            }
            set
            {
                _currentUser = value;
                RaisePropertyChanged("CurrentUser");
            }
        }

        public bool IsCurrentCanGet(string powerName)
        {
            return false;
        }



        /// <summary>
        /// Is submitting data when logon
        /// </summary>
        private bool _isActive;
        public bool IsActive
        {
            get
            {
                return _isActive;
            }
            set
            {
                _isActive = value;
                RaisePropertyChanged("IsActive");
            }
        }

        private bool _isShowLoginPanel = true;
        public bool IsShowLoginPanel
        {
            get
            {
                return _isShowLoginPanel;
            }
            set
            {
                _isShowLoginPanel = value;
                RaisePropertyChanged("IsShowLoginPanel");
            }
        }

        private DateTime _loginTime = DateTime.Now;
        public DateTime LoginTime
        {
            get
            {
                return _loginTime;
            }
            set
            {
                _loginTime = value;
                RaisePropertyChanged("LoginTime");
            }
        }

        protected ICommand userLogin;
        public virtual ICommand UserLogin
        {
            get
            {

                if (userLogin == null)
                    userLogin = new RelayCommand<DataForm>(dataform =>
                    {
                        if (dataform.ValidateItem())
                        {
                            this.IsActive = true;

                            this.DomainContext.Load<MembershipUser>(this.DomainContext.GetMembershipUserIncludingPowerQuery().Where(u => u.UserName == CurrentUser.UserName),
                                o =>
                                    {
                                        IsActive = false;

                                        var user = o.Entities.FirstOrDefault();
                                        if (user == null || user.Pwd != CurrentUser.Pwd
                                            )
                                        {
                                            Window.Alert("Wrong user name or password.");
                                        }
                                        else
                                        {
                                            this.IsShowLoginPanel = false;
                                            this.CurrentUser = user;
                                            this.LoginTime = DateTime.Now;

                                            var powers = new Dictionary<string, bool>();
                                            foreach (var rolePower in this.CurrentUser.MembershipRole.MembershipRolePower)
                                            {
                                                powers.Add(rolePower.MembershipPower.Name.Trim() + ".Get", rolePower.GetFlag == true);
                                                powers.Add(rolePower.MembershipPower.Name.Trim() + ".Add", rolePower.AddFlag == true);
                                                powers.Add(rolePower.MembershipPower.Name.Trim() + ".Del", rolePower.DelFlag == true);
                                                powers.Add(rolePower.MembershipPower.Name.Trim() + ".Edit", rolePower.EditFlag == true);
                                                powers.Add(rolePower.MembershipPower.Name.Trim() + ".Exec", rolePower.ExecFlag == true);
                                            }
                                            this.CurrentPowers = powers;
                                        }

                                    }, false);
                        }
                    });

                return userLogin;
            }
        }

        public override void Init()
        {
            base.Init();

            //load all power list in order to bind RelationshipNavigationProperty of  MembershipRolePower
            this.DomainContext.Load<MembershipPower>(this.DomainContext.GetMembershipPowerQuery());

            //this.IsShowLoginPanel = false;
        }
    }

}